package com.redoor.app.stake.auth.controller;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.auth.model.serviceModel.BaseUserServiceBean;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.model.BaseUserCriteria;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* Created by wangjunlei on 2018-02-07 15:19:13.
*/
@RestController
@RequestMapping("/base/users")
public class BaseUserController {

    @Autowired
    private BaseUserService baseUserService;

    private final Logger logger = LoggerFactory.getLogger(BaseUserController.class);

    @PostMapping
    public ResponseResult add(@RequestBody BaseUser baseUser) {
        baseUserService.save(baseUser);
        return ResultGenerator.generatorSuccess();
    }

    @PutMapping
    public ResponseResult update(@RequestBody BaseUser baseUser) {
        baseUserService.updateBySelective(baseUser);
        return ResultGenerator.generatorSuccess();
    }
    @ApiOperation(value = "根据传入用户uuid查询对象,传值:uuid='主键'", notes = "根据传入用户uuid查询对象")
    @PostMapping("/detail")
    public ResponseResult detail(String uuid) {
        BaseUser baseUser = baseUserService.findById(uuid);
        return ResultGenerator.generatorSuccess(baseUser);
    }

    @GetMapping
    public ResponseResult list(BaseUserCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BaseUser> list = baseUserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.generatorSuccess(pageInfo);
    }

    /**
     * 主页面查询
     *
     * @Author zhangYiHang
     * @Date 2018/3/5 9:55
     */
    @ApiOperation(value = "/list", notes = "传入账号与用户名称查询")
    @RequiresPermissions("sys:user:list")
    @GetMapping("/list")
    public ResponseResult queryList(String fullname, String loginname) {
        BaseUser baseUser = new BaseUser();
        baseUser.setFullname(fullname);
        baseUser.setLoginname(loginname);
        List<BaseUser> list = this.baseUserService.qryUser(baseUser);
        return ResultGenerator.generatorSuccess(list);
    }
    /**
     * 传入为主键UUID删除用户
     *
     * @Author zhangYiHang
     * @Date 2018/3/5 9:58
     */
    @ApiOperation(value = "通过uuid删除用户",notes = "通过uuid删除用户")
    @RequiresPermissions("sys:user:delete")
    @PostMapping("/del")
    public ResponseResult deleteUser(String uuid) {
        try {
            Integer status = this.baseUserService.updateStatusByUuid(uuid);
            if (status > 0){
                logger.info("用户删除成功");
                return ResultGenerator.generatorSuccess();
            }else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            logger.error("用户删除失败");
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * @Author zhangYiHang
     * @Date 2018/3/7 11:51
     * 修改用户密码,传入为用户UUid以及新旧密码
     */
    @ApiOperation(value = "updatePassword",notes = "通过用户UUid以及原始密码修改用户密码")
    @RequiresPermissions("sys:user:updatePassword")
    @PostMapping("/updatePassword")
    public ResponseResult updatePassword(String uuid, String oldPassword, String password){
        try {
            BaseUserServiceBean baseUserActionBean = new BaseUserServiceBean();
            baseUserActionBean.setUuid(uuid);
            baseUserActionBean.setOldPassword(oldPassword);
            baseUserActionBean.setPassword(password);
            int status = this.baseUserService.updateByUserUuidAndPassword(baseUserActionBean);
            //判断修改语句是否执行,如果执行则大于0,返回成功
            if(status > 0){
                return ResultGenerator.generatorSuccess();
            }else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 用户修改
     * @Author zhangYiHang
     * @Date 2018/3/13 20:19
     */
    @ApiOperation(value = "update",notes = "用户修改")
    @RequiresPermissions("sys:user:update")
    @PostMapping("/update")
    public ResponseResult updateUser(BaseUserCriteria baseUserCriteria){
        try {
            BaseUserServiceBean baseUserActionBean = new BaseUserServiceBean();
            BaseUser loginUser = this.baseUserService.getShiroUser(null);
            baseUserActionBean.setUpdateruuid(loginUser.getUuid());
            baseUserActionBean.setUpdatedat(new Date());
            baseUserActionBean.setPassword(baseUserCriteria.getPassword());
            baseUserActionBean.setLoginname(baseUserCriteria.getLoginname());
            baseUserActionBean.setPhone(baseUserCriteria.getPhone());
            baseUserActionBean.setFullname(baseUserCriteria.getFullname());
            baseUserActionBean.setRoleUuids(baseUserCriteria.getRoleUuids());
            baseUserActionBean.setUuid(baseUserCriteria.getUuid());
            int status = this.baseUserService.updateByUuid(baseUserActionBean);
            //修改执行两次SQL语句
            if (status > 1) {
                return ResultGenerator.generatorSuccess();
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            logger.error("用户信息修改失败,执行时间"+ new Date());
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     * 添加用户
     * @param baseUserCriteria
     * @return
     */
    @ApiOperation(value = "insertUser",notes = "添加用户")
    @RequiresPermissions("sys:user:insert")
    @PostMapping("/insertUser")
    public  ResponseResult insertUser(BaseUserCriteria baseUserCriteria){
        try {
            BaseUserServiceBean baseUserActionBean = new BaseUserServiceBean();
            BaseUser loginUser = this.baseUserService.getShiroUser(null);
            baseUserActionBean.setCreatoruuid(loginUser.getUuid());
            baseUserActionBean.setCreatedat(new Date());
            baseUserActionBean.setUuid(UUID.randomUUID().toString());
            baseUserActionBean.setStatus(1000);
            baseUserActionBean.setLoginname(baseUserCriteria.getLoginname());
            baseUserActionBean.setFullname(baseUserCriteria.getFullname());
            baseUserActionBean.setPhone(baseUserCriteria.getPhone());
            baseUserActionBean.setPassword(baseUserCriteria.getPassword());
            baseUserActionBean.setRoleUuids(baseUserCriteria.getRoleUuids());
           int status = this.baseUserService.insertBaseUser(baseUserActionBean);
           //后台为两次数据插入全部执行为执行成功
            if (status > 1) {
                return ResultGenerator.generatorSuccess(StakeApplicationStatus.SUCCESS);
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据用户uuid密码重置
     * @Author zhangYiHang
     * @Date 2018/3/9 10:45
     */
    @ApiOperation(value = "revicePassword",notes = "根据传入uuid密码重置为用户名")
    @PostMapping("/revice")
    public ResponseResult revicePassword(String uuid){
        try {
           int i = this.baseUserService.revicePassword(uuid);
           if (i >0){
               return  ResultGenerator.generatorSuccess(StakeApplicationStatus.SUCCESS);
           }else {
               return  ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
           }
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }
    /**
     * 根据用户名称查询用户
     * @Author zhangYiHang
     * @Date  16:45
     */
    @ApiOperation(value = "qryUserByLongname",notes = "根据用户名称查询用户")
    @PostMapping("/qryUserByLongname")
    public ResponseResult qryUserByLoginname(String loginname){
        BaseUserServiceBean baseUserAction = new BaseUserServiceBean();
        baseUserAction.setLoginname(loginname);
        BaseUser user = this.baseUserService.findByUsername(baseUserAction.getLoginname());
        return ResultGenerator.generatorSuccess(user);
    };
}
